package com.example.pathprotectsystem.service.impl;

import com.example.pathprotectsystem.mapper.RecordMapper;
import com.example.pathprotectsystem.pojo.AskList;
import com.example.pathprotectsystem.pojo.PageBean;
import com.example.pathprotectsystem.pojo.Path;
import com.example.pathprotectsystem.pojo.ResultModel;
import com.example.pathprotectsystem.service.RecordService;
import com.example.pathprotectsystem.utils.LiQingCentCalculateUtil;
import com.example.pathprotectsystem.utils.ShuiniCentCalculateUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RecordServiceImpl implements RecordService {
    @Autowired
    private RecordMapper recordMapper;

    @Override
    public int findTaskIdFromf1(int id) {
        return recordMapper.findTaskIdFromf1(id);
    }

    @Override
    public int findTaskIdFromf2(int id) {
        return recordMapper.findTaskIdFromf2(id);
    }

    @Override
    public int findTaskIdFromf3(int id) {
        return recordMapper.findTaskIdFromf3(id);
    }

    @Override
    public int findTaskIdFromf4(int id) {
        return recordMapper.findTaskIdFromf4(id);
    }

    @Override
    public PageBean<ResultModel> f1list(AskList askList) {
        PageBean<ResultModel> pb = new PageBean<>();
        Integer pageNum = askList.getPageNum();
        Integer pageSize = askList.getPageSize();
        String str = askList.getStr();
        //开启分页查询 ,PageHelper是个插件
        PageHelper.startPage(pageNum,pageSize);
        List<ResultModel> as = recordMapper.f1list(str);
        System.out.println(as.size());
        //Page中提供了方法，可以获取PageHelper分页查询后  得到的总记录条数和当前页数据
        Page<ResultModel> p = (Page<ResultModel>) as;
        //把数据填充到PageBean对象中
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }

    @Override
    public void deletef1(int id) {
        recordMapper.deletef1(id);
    }

    @Override
    public void deletef2(int id) {
        recordMapper.deletef2(id);
    }

    @Override
    public PageBean<ResultModel> f2list(AskList askList) {
        PageBean<ResultModel> pb = new PageBean<>();
        Integer pageNum = askList.getPageNum();
        Integer pageSize = askList.getPageSize();
        String str = askList.getStr();
        //开启分页查询 ,PageHelper是个插件
        PageHelper.startPage(pageNum,pageSize);
        List<ResultModel> as = recordMapper.f2list(str);
        System.out.println(as.size());
        //Page中提供了方法，可以获取PageHelper分页查询后  得到的总记录条数和当前页数据
        Page<ResultModel> p = (Page<ResultModel>) as;
        //把数据填充到PageBean对象中
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }

    @Override
    public void deletef3(int id) {
        recordMapper.deletef3(id);
    }

    @Override
    public PageBean<ResultModel> f3list(AskList askList) {
        PageBean<ResultModel> pb = new PageBean<>();
        Integer pageNum = askList.getPageNum();
        Integer pageSize = askList.getPageSize();
        String str = askList.getStr();
        //开启分页查询 ,PageHelper是个插件
        PageHelper.startPage(pageNum,pageSize);
        List<ResultModel> as = recordMapper.f3list(str);
        System.out.println(as.size());
        //Page中提供了方法，可以获取PageHelper分页查询后  得到的总记录条数和当前页数据
        Page<ResultModel> p = (Page<ResultModel>) as;
        //把数据填充到PageBean对象中
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }

    @Override
    public void deletef4(int id) {
        recordMapper.deletef4(id);
    }

    @Override
    public PageBean<ResultModel> f4list(AskList askList) {
        PageBean<ResultModel> pb = new PageBean<>();
        Integer pageNum = askList.getPageNum();
        Integer pageSize = askList.getPageSize();
        String str = askList.getStr();
        //开启分页查询 ,PageHelper是个插件
        PageHelper.startPage(pageNum,pageSize);
        List<ResultModel> as = recordMapper.f4list(str);
        System.out.println(as.size());
        //Page中提供了方法，可以获取PageHelper分页查询后  得到的总记录条数和当前页数据
        Page<ResultModel> p = (Page<ResultModel>) as;
        //把数据填充到PageBean对象中
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }

    @Override
    public PageBean<ResultModel> f5list(AskList askList) {
        PageBean<ResultModel> pb = new PageBean<>();
        Integer pageNum = askList.getPageNum();
        Integer pageSize = askList.getPageSize();
        String str = askList.getStr();
        //开启分页查询 ,PageHelper是个插件
        PageHelper.startPage(pageNum,pageSize);
        List<ResultModel> as = recordMapper.f5list(str);
        for(int i=0;i<as.size();i++){
            String s = as.get(i).getStringstr().substring(0,10);
            String sheetName = as.get(i).getPavementType();
            if(sheetName.equals("沥青路面")){
                as.get(i).setSheetType("沥青路面损坏调查表");
            }
            else{
                as.get(i).setSheetType("水泥混凝土路面损坏调查表");
            }
            as.get(i).setFinishTime(s);
        }
        System.out.println(as);
        //Page中提供了方法，可以获取PageHelper分页查询后  得到的总记录条数和当前页数据
        Page<ResultModel> p = (Page<ResultModel>) as;
        //把数据填充到PageBean对象中
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }

    @Override
    public List<ResultModel> pavementdamagesheetlist(String finishTime,String userName,String pathName) {
        List<ResultModel> p = recordMapper.pavementdamagesheetlist();
        for(int i=0;i<p.size();i++){
            String f = p.get(i).getFinishTime();
            String u = p.get(i).getUserName();
            String pa = p.get(i).getPathName();
            String damageType = p.get(i).getDamageType();
            double damageP = p.get(i).getDamageP();
            System.out.println(f+" "+u+" "+pa+" ");
            if(f==null&&u==null&&pa==null){
                p.get(i).setDamageArea(0);
                p.get(i).setDamageP(0);
                p.get(i).setComment("");
                System.out.println("null "+i);
                continue;
            }
            if(f.equals(finishTime)&&u.equals(userName)&&pa.equals(pathName)){
                int c = LiQingCentCalculateUtil.getDownCent(damageType,damageP);
                p.get(i).setDownCent(c);
                System.out.println("all= "+i);
            }
            else{
                p.get(i).setDamageArea(0);
                p.get(i).setDamageP(0);
                p.get(i).setComment("");
                System.out.println("all!= "+i);
            }
        }
        return p;
    }

    @Override
    public List<ResultModel> shuinipavementdamagesheetlist(String finishTime, String userName, String pathName) {
        List<ResultModel> p = recordMapper.shuinipavementdamagesheetlist();
        for(int i=0;i<p.size();i++){
            String f = p.get(i).getFinishTime();
            String u = p.get(i).getUserName();
            String pa = p.get(i).getPathName();
            String damageType = p.get(i).getDamageType();
            double damageP = p.get(i).getDamageP();
            System.out.println(f+" "+u+" "+pa+" ");
            if(f==null&&u==null&&pa==null){
                p.get(i).setDamageArea(0);
                p.get(i).setDamageP(0);
                p.get(i).setComment("");
                System.out.println("null "+i);
                continue;
            }
            if(f.equals(finishTime)&&u.equals(userName)&&pa.equals(pathName)){
                int c = ShuiniCentCalculateUtil.getDownCent(damageType,damageP);
                p.get(i).setDownCent(c);
                System.out.println("all= "+i);
            }
            else{
                p.get(i).setDamageArea(0);
                p.get(i).setDamageP(0);
                p.get(i).setComment("");
                System.out.println("all!= "+i);
            }
        }
        return p;
    }

}
